/*
 *  spa.memo.js
 *  메모와 관련된 조작.
 */

spa.memo = (function() {
/*-----------------------Module Variable Start----------------------*/
  var configMap = {
    main_html : String()
      + '<h1>MEMO</h1>'
      + '<div class="memo_content_wrap">'
        + '<textarea id="memo_content"></textarea>'
        + '<div id="memo_list"></div>'
      +  '</div>'
      + '<p>'
        + '<input class="btn btn-primary spa-memo-btn_save" type="button" value="Save" />'
        + '<input class="btn btn-primary spa-memo-btn_load" type="button" value="Load" />'
      + '</p>'
      + '<p><input class="btn btn-warning spa-memo-btn_slide_down" type="button" value="내리기" /></p>',
      slide_activate_top : "-29em",
      slide_activate_height : "30em",
      slide_deactivate_top : "6px",
      slide_deactivate_height : "20px",
      slide_activate_duration : 150,
      slide_deactivate_duration : 150
  },
  stateMap = {
    $container : null,
    is_memo_slide : true
  },
  jqueryMap = {},
  initModule, setJqueryMap, getIsMemoSlide,
  slideToggle, saveMemo, loadMemo,  setMemoList, selectMemo;
/*-----------------------Module Variable End------------------------*/
    
/*-----------------------Utility Method Start-----------------------*/
/*-----------------------Utility Method End-------------------------*/

/*-----------------------DOM Method Start---------------------------*/
  // /setJqueryMap/ Start
  setJqueryMap = function() {
    var $container = stateMap.$container;
    jqueryMap = {
      $container : $container
    };
  };
  // /setJqueryMap/ End
  // /setMemoList/ Start
  setMemoList = function( json ) {
    if( json[0] === undefined ) {
      alert( '로그인하지 않았거나 아무런 메모도 저장하지 않았습니다.' );
      return false;
    }
    var i = 0,
        list_html = String() + '<ul>';

    for(i = 0; i < json.length; i++) {
      list_html = list_html
        + '<li><a href="javascript:spa.memo.selectMemo(' + i + ')">'
          + json[i].time
        + '</a></li>'
    }

    list_html = list_html + '</ul>';
    
    $('#memo_content').hide();
    $('#memo_list').html(list_html).show();
  }
  // /setMemoList/ End
/*-----------------------DOM Method End-----------------------------*/
    
/*-----------------------Event Handler Start------------------------*/
  saveMemo = function(e) {
    spa.model.memo.save($('#memo_content').val());
  };
  loadMemo = function(e) {
    var request = spa.model.memo.load(1);
    $('#memo_content').val("");
    request.success(function() {
      setMemoList(spa.model.memo.getJSON());
    });
  };
/*-----------------------Event Handler End--------------------------*/

/*-----------------------Public Method Start------------------------*/
  // /initModule/ Start
  initModule = function($container) {
    stateMap.$container = $container;
    $container.html(configMap.main_html);
    setJqueryMap();
    $('.spa-memo-btn_save').on('click', saveMemo);
    $('.spa-memo-btn_load').on('click', loadMemo);
  };
  // /initModule/ End
  // /slideToggle/ Start
  slideToggle = function( is_slide, callback ) {
    
    if ( is_slide ) {
      stateMap.$container.animate(
          {
            top : configMap.slide_activate_top,
            height: configMap.slide_activate_height
          },
          configMap.slide_activate_duration,
          function() {
            stateMap.is_memo_slide = false;
            if ( callback ) { callback(); }
          }
      );
      return true;
    }
    else {
      stateMap.$container.animate(
          {
            top : configMap.slide_deactivate_top,
            height: configMap.slide_deactivate_height
          },
          configMap.slide_deactivate_duration,
          function() {
            stateMap.is_memo_slide = true;
            if ( callback ) { callback(); }
          }
      );
      return true;
    }

    return false;
  };
  // /slideToggle/ End
  // /getIsMemoSlide/ Start
  getIsMemoSlide = function() {
    return stateMap.is_memo_slide;
  };
  // /getIsMemoSlide/ End
  // /selectMemo/ Start
  selectMemo = function( index ) {
    var json = spa.model.memo.getJSON();
    $('#memo_list').hide();
    $('#memo_content').val(json[index].text).show();
  };
  // /selectMemo/ End
/*-----------------------Public Method End--------------------------*/
  // Return public function
  return {
    initModule : initModule,
    slideToggle : slideToggle,
    getIsMemoSlide : getIsMemoSlide,
    selectMemo : selectMemo
  };
}());
